package com.koron.system.menu.controller;

import com.koron.bean.base.BaseQueryBean;
import com.koron.bean.base.Response;
import com.koron.bean.system.menu.MenuBean;
import com.koron.bean.system.menu.MenuFuncBean;
import com.koron.common.core.business.datadict.bean.po.Lang;
import com.koron.common.core.util.FastJsonUtils;
import com.koron.system.menu.mapper.MenuMapper;
import com.koron.system.menu.service.MenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.koron.ebs.mybatis.ADOConnection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName MenuController
 * @description
 * @Author zhouj
 * @Date 2021/7/22 18:22
 */
@RestController
@RequestMapping("/system/menu")
@Slf4j
@Api(tags = "菜单管理")
public class MenuController {

    @Autowired
    MenuService menuService;

    @ApiOperation(value = "新增菜单")
    @PostMapping("/insertMenu")
    public Response insertMenu(@RequestBody MenuBean menuBean) {
        return Response.success(menuService.insertMenu(menuBean ));
    }

    @ApiOperation(value = "修改菜单")
    @PutMapping("/updateMenu")
    public Response updateMenu(@RequestBody MenuBean menuBean) {
        return Response.success(menuService.updateMenu(menuBean ));
    }

    @ApiOperation(value = "删除菜单")
    @DeleteMapping("/delete")
    public Response deleteMenuById(String id) {
        return Response.success(menuService.deleteMenuById(id));
    }

    @ApiOperation(value = "删除菜单list")
    @DeleteMapping("/deleteList")
    public Response deleteMenuList(@RequestParam("ids") List<String> ids) {
        return Response.success(menuService.deleteMenuList(ids));
    }

    @ApiOperation(value = "查询菜单树")
    @GetMapping("/queryMenuTree")
    public Response queryMenuTree(MenuBean menuBean) {
        return Response.success(menuService.queryMenuTree(menuBean));
    }

    @ApiOperation(value = "查询移动菜单树")
    @GetMapping("/queryAppMenuTree")
    public Response queryAppMenuTree(MenuBean menuBean) {
        return Response.success(menuService.queryAppMenuTree(menuBean));
    }

    @ApiOperation(value = "查询输电风控移动菜单树")
    @GetMapping("/queryLineAppMenuTree")
    public Response queryLineAppMenuTree(MenuBean menuBean) {
        return Response.success(menuService.queryLineAppMenuTree(menuBean));
    }

    @ApiOperation(value = "根据父节点查询菜单列表")
    @GetMapping("/queryMenuList")
    public Response queryMenuList(MenuBean menuBean , BaseQueryBean queryBean) {
        return Response.success(menuService.queryMenuList(menuBean , queryBean), menuService.queryMenuListCount(menuBean));
    }

    @ApiOperation(value = "查询单个菜单信息")
    @GetMapping("/querySingleMenu")
    public Response<MenuBean> querySingleMenu(MenuBean menuBean ) {
        return Response.success(menuService.querySingleMenu(menuBean));
    }

    @GetMapping("/tempForm")
    @ApiOperation("更新多语种数据(菜单)")
    public Response tempUpdata() {
        ADOConnection.runTask(sessionFactory -> {
            MenuMapper mapper = sessionFactory.getMapper(MenuMapper.class);
            List<MenuBean> all = mapper.queryAll();

            for (MenuBean d : all) {
                Lang lang = new Lang();
                lang.setZhCn(d.getName());
                lang.setZhHk(d.getZhHk());
                lang.setEn(d.getEn());
                String beanToJson = FastJsonUtils.getBeanToJson(lang);
                log.info(beanToJson);
                d.setLang(beanToJson);
            }
            mapper.updateBatch(all);
            return true;
        }, Boolean.class);
        return Response.success();
    }

    @GetMapping("/tempButton")
    @ApiOperation("更新多语种数据(按钮)")
    public Response tempButton() {
        ADOConnection.runTask(sessionFactory -> {
            MenuMapper mapper = sessionFactory.getMapper(MenuMapper.class);
            List<MenuFuncBean> all = mapper.queryMenuFunAll();

            for (MenuFuncBean d : all) {
                Lang lang = new Lang();
                lang.setZhCn(d.getName());
                lang.setZhHk(d.getZhHk());
                lang.setEn(d.getEn());
                String beanToJson = FastJsonUtils.getBeanToJson(lang);
                log.info(beanToJson);
                d.setLang(beanToJson);
            }
            mapper.updateMenFunBatch(all);
            return true;
        }, Boolean.class);
        return Response.success();
    }

}
